package com.findmeastore.action;

import java.util.ArrayList;
import java.util.List;

import com.findmeastore.bc.IBrandBc;
import com.findmeastore.bc.IProductBc;
import com.findmeastore.bo.BrandBo;
import com.findmeastore.bo.CategoryBo;
import com.findmeastore.bo.ProductBo;
import com.findmeastore.dto.BrandDto;

public class BrandCrudAction {
	private Integer id;
	private BrandDto brandDto;
	private IBrandBc brandBc;
	private IProductBc productBc;
	private List<BrandBo> brands;
	private List<ProductBo> products;
	private String action;

	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}

	public String list() {
		setBrands(brandBc.getAllBrands());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			BrandBo brandBo = brandBc.getBrandById(id);
			if (brandBo != null) {
				brandDto = new BrandDto();
				brandDto.setId(id);
				brandDto.setName(brandBo.getName());
				List<Integer> productIds = new ArrayList<Integer>();
				for(ProductBo productBo:brandBo.getProducts()){
					productIds.add(productBo.getId());
				}
				brandDto.setProductIds(productIds.toArray(new Integer[]{}));
			}
		}
		setProducts(productBc.getAllProducts());
		return "EDIT";
	}

	public String save() {
		if (brandDto.getId() == null) {
			brandDto.setCreatedById(1);
		}
		brandDto.setModifiedById(1);
		brandBc.saveBrand(brandDto);
		return "REDIRECT";
	}

	public BrandDto getBrandDto() {
		return brandDto;
	}

	public void setBrandDto(BrandDto brandDto) {
		this.brandDto = brandDto;
	}

	public IBrandBc getBrandBc() {
		return brandBc;
	}

	public void setBrandBc(IBrandBc brandBc) {
		this.brandBc = brandBc;
	}

	public List<BrandBo> getBrands() {
		return brands;
	}

	public void setBrands(List<BrandBo> brands) {
		this.brands = brands;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<ProductBo> getProducts() {
		return products;
	}

	public void setProducts(List<ProductBo> products) {
		this.products = products;
	}

	public IProductBc getProductBc() {
		return productBc;
	}

	public void setProductBc(IProductBc productBc) {
		this.productBc = productBc;
	}
}
